package com.xb.cloud.shopping.service.impl;

import java.math.BigDecimal;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.xb.cloud.common.util.UUidUtils;
import com.xb.cloud.gateway.client.ReturnCodes;
import com.xb.cloud.gateway.client.ReturnUtils;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.order.service.client.PaymentService;
import com.xb.cloud.order.service.client.SellOrderService;
import com.xb.cloud.order.service.client.bo.SoDeliveryBo;
import com.xb.cloud.service.client.ServiceReturnInfo;
import com.xb.cloud.service.client.ServiceReturnUtils;
import com.xb.cloud.shopping.model.PaymentConfig;
import com.xb.cloud.shopping.service.PaymentApiService;
import com.xb.cloud.shopping.service.PaymentDelegateService;
import com.xb.cloud.shopping.service.client.vo.PaymentRequestVo;
import com.xb.cloud.shopping.service.client.vo.PaymentShopInfo;

@Service
public class PaymentApiServiceImpl implements PaymentApiService {
	@Resource
	PaymentService paymentService;

	@Resource
	PaymentDelegateService paymentDelegateService;

	@Resource
	SellOrderService sellOrderService;

	@Override
	public RtnInfo getReceiptCode(PaymentRequestVo requestVo) {
		ServiceReturnInfo serviceReturnInfo = paymentService.getShopPaymentInfo(requestVo);
		if (ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			return ReturnUtils.success(serviceReturnInfo.getData());
		} else {
			return ReturnUtils.error(ReturnCodes.PAYMENTCODE_NOT_EXIST);
		}
	}

	@Override
	public RtnInfo checkPaymentNotify(Map<String, String> params) {
		return paymentDelegateService.checkPaymentNotify(params);
	}

	@Override
	public RtnInfo payOrder(PaymentRequestVo requestVo) {
		ServiceReturnInfo serviceReturnInfo = paymentService.getShopPaymentInfo(requestVo);
		if (ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			PaymentShopInfo paymentShopInfo = (PaymentShopInfo) serviceReturnInfo.getData();
			String openId = paymentShopInfo.getOpenId();
			String openKey = paymentShopInfo.getOpenKey();

			Long orderId = requestVo.getOrderId();
			SoDeliveryBo orderInfo = sellOrderService.getOrderDetailsById(orderId);
			BigDecimal orderAmount = orderInfo.getSoSummaryBo().getOrderAmount();

			Integer tradeAmount = 0;
			if (orderAmount != null) {
				// 去除两位小数，换算成整数
				tradeAmount = orderAmount.multiply(BigDecimal.valueOf(100)).intValue();
			}

			String outNo = UUidUtils.getUUID();
			String ordName = "订单支付：" + paymentShopInfo.getShopName();
			Integer ignoreAmount = 0;
			Integer discountAmount = 0;
			Integer originalAmount = tradeAmount;
			String pmtTag = requestVo.getPmtTag();
			String pmtName = requestVo.getPmtName();
			String notifyUrl = PaymentConfig.NOTIFY_URL;
			String jumpUrl = PaymentConfig.JUMP_URL;

			return paymentDelegateService.payOrder(requestVo, openId, openKey, outNo, pmtTag, pmtName, ordName,
					originalAmount, discountAmount, ignoreAmount, tradeAmount, "", "", notifyUrl, jumpUrl);

		} else {
			return ReturnUtils.error(ReturnCodes.PAYMENTCODE_NOT_EXIST);
		}
	}

	@Override
	public RtnInfo getPayList(PaymentRequestVo requestVo) {
		ServiceReturnInfo serviceReturnInfo = paymentService.getShopPaymentInfo(requestVo);
		if (ServiceReturnUtils.isSuccess(serviceReturnInfo)) {
			PaymentShopInfo paymentShopInfo = (PaymentShopInfo) serviceReturnInfo.getData();
			String openId = paymentShopInfo.getOpenId();
			String openKey = paymentShopInfo.getOpenKey();

			return paymentDelegateService.getPayList(openId, openKey);
		} else {
			return ReturnUtils.error(ReturnCodes.PAYMENTCODE_NOT_EXIST);
		}

	}

}
